// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include <stdlib.h>
#include "commoncppproblem129.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=129 lang=cpp
 *
 * [129] 求根到叶子节点数字之和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void preorder(TreeNode*root,string path,vector<string> &res)
    {
        if(root == NULL)
            return ;
        path += to_string(root->val);
        if(root->left == NULL && root->right == NULL)
        {
            res.push_back(path);
        }
        else
        {
            preorder(root->left,path,res);
            preorder(root->right,path,res);
        }
    }
    int sumNumbers(TreeNode* root) {
        vector<string> res;
        preorder(root,"",res);
        int Sum = 0;
        for(auto it = res.begin();it!=res.end();it++)
        {
            Sum += atoi(it->c_str());
        }
        return Sum;
    }
};
// @lc code=end

